package com.jt.assistant.viewmodel

import androidx.lifecycle.MutableLiveData
import com.jt.assistant.base.BaseResult
import com.jt.assistant.base.GlobalConfig
import com.jt.assistant.bean.*
import com.jt.assistant.repository.HomeRepository
import com.orhanobut.logger.Logger
import com.yzp.mvvmlibrary.base.BaseViewModel
import kotlinx.coroutines.delay

class HomeViewModel : BaseViewModel() {

    private val homeRepository by lazy {
        HomeRepository.getInstance()
    }


    val mLogin = MutableLiveData<BaseResult<LoginDataBean>>()
    fun login(userName: String, password: String): MutableLiveData<BaseResult<LoginDataBean>> {
        val params = Param::class.java.newInstance()
        params.userName = userName
        params.password = password
        launchGo({
            mLogin.value = homeRepository.login(params)
            if (!mLogin.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mLogin.value!!.errorMsg)
            }
        }, true)
        return mLogin
    }


    val mGetScanList = MutableLiveData<BaseResult<ScanDataBean>>()
    fun getScanList(map: Map<String, String>): MutableLiveData<BaseResult<ScanDataBean>> {
        launchGo({
            mGetScanList.value = homeRepository.getScanList(map)
            if (!mGetScanList.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetScanList.value!!.errorMsg)
            }
        }, true)
        return mGetScanList
    }

    val mSendScanLog = MutableLiveData<BaseResult<Any>>()
    val mScanCarton = MutableLiveData<MutableList<ScanCarton>>()
    fun sendScanLog(list: MutableList<ScanCarton>): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mSendScanLog.value = homeRepository.sendScanLog(list)
            if (!mSendScanLog.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mSendScanLog.value!!.errorMsg)
            } else {
                mScanCarton.value = list
            }
        }, false)
        return mSendScanLog
    }

    val mOneKeyScan = MutableLiveData<BaseResult<Any>>()
    fun oneKeyScan(list: MutableList<OnKeyScanCarton>): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mOneKeyScan.value = homeRepository.oneKeyScan(list)
            if (!mOneKeyScan.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mOneKeyScan.value!!.errorMsg)
            }
        }, true)
        return mOneKeyScan
    }

    val mOnekeyScanQuery = MutableLiveData<BaseResult<Boolean>>()
    fun onekeyScanQuery(list: MutableList<OnKeyScanCarton>): MutableLiveData<BaseResult<Boolean>> {
        launchGo({
            mOnekeyScanQuery.value = homeRepository.onekeyScanQuery(list)
            if (!mOnekeyScanQuery.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mOnekeyScanQuery.value!!.errorMsg)
            }
        }, false)
        return mOnekeyScanQuery
    }

    val mModifyWaybill = MutableLiveData<BaseResult<Any>>()
    fun modifyWaybill(param: Param): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mModifyWaybill.value = homeRepository.modifyWaybill(param)
            if (!mModifyWaybill.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mModifyWaybill.value!!.errorMsg)
            }
        }, true)
        return mModifyWaybill
    }

    val mGetDeliverTruck = MutableLiveData<BaseResult<MutableList<AddCarSearchBean>>>()
    fun getDeliverTruck(): MutableLiveData<BaseResult<MutableList<AddCarSearchBean>>> {
        launchGo({
            mGetDeliverTruck.value = homeRepository.getDeliverTruck()
            if (!mGetDeliverTruck.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetDeliverTruck.value!!.errorMsg)
            }
        }, true)
        return mGetDeliverTruck
    }

    val mGetCarList = MutableLiveData<BaseResult<CarSearchBean>>()
    fun getCarList(param: Param): MutableLiveData<BaseResult<CarSearchBean>> {
        launchGo({
            mGetCarList.value = homeRepository.getCarList(param)
            if (!mGetCarList.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetCarList.value!!.errorMsg)
            }
        }, true)
        return mGetCarList
    }

    val mGetCarPlanList = MutableLiveData<BaseResult<CarSelfBean>>()
    fun getCarPlanList(param: Param): MutableLiveData<BaseResult<CarSelfBean>> {
        launchGo({
            mGetCarPlanList.value = homeRepository.getCarPlanList(param)
            if (!mGetCarPlanList.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetCarPlanList.value!!.errorMsg)
            }
        }, true)
        return mGetCarPlanList
    }


    val mGetDic = MutableLiveData<BaseResult<MutableList<DicBean>>>()
    fun getDic(param: Param): MutableLiveData<BaseResult<MutableList<DicBean>>> {
        launchGo({
            mGetDic.value = homeRepository.getDic(param)
            if (!mGetDic.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetDic.value!!.errorMsg)
            }
        }, true)
        return mGetDic
    }

    val mAddDeliverCar = MutableLiveData<BaseResult<Any>>()
    fun addDeliverCar(param: Param): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mAddDeliverCar.value = homeRepository.addDeliverCar(param)
            if (!mAddDeliverCar.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mAddDeliverCar.value!!.errorMsg)
            }
        }, true)
        return mAddDeliverCar
    }

    val mGenerateReceipt = MutableLiveData<BaseResult<Any>>()
    fun generateReceipt(param: Param): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mGenerateReceipt.value = homeRepository.generateReceipt(param)
            if (!mGenerateReceipt.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGenerateReceipt.value!!.errorMsg)
            }
        }, true)
        return mGenerateReceipt
    }

    val mGetStoreCityList = MutableLiveData<BaseResult<MutableList<CityBean>>>()
    fun getStoreCityList(): MutableLiveData<BaseResult<MutableList<CityBean>>> {
        launchGo({
            mGetStoreCityList.value = homeRepository.getStoreCityList()
            if (!mGetStoreCityList.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetStoreCityList.value!!.errorMsg)
            }
        }, true)
        return mGetStoreCityList
    }

    val mUploadForwarderWaybill = MutableLiveData<BaseResult<Any>>()
    fun uploadForwarderWaybill(
        param: Param,
        picList: MutableList<String>
    ): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mUploadForwarderWaybill.value = homeRepository.uploadForwarderWaybill(
                param,
                picList
            )
            if (!mUploadForwarderWaybill.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mUploadForwarderWaybill.value!!.errorMsg)
            }
        }, true)
        return mUploadForwarderWaybill
    }

    val mWareFile = MutableLiveData<BaseResult<Any>>()
    fun wareFile(
        param: Param,
        picList: MutableList<String>
    ): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mWareFile.value = homeRepository.wareFile(
                param,
                picList
            )
            if (!mWareFile.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mWareFile.value!!.errorMsg)
            }
        }, true)
        return mWareFile
    }


    val mGetWaybillSkuList = MutableLiveData<BaseResult<Any>>()
    fun getWaybillSkuList(
        param: Map<String, String>
    ): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mGetWaybillSkuList.value = homeRepository.getWaybillSkuList(param)
            if (!mGetWaybillSkuList.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetWaybillSkuList.value!!.errorMsg)
            }
        }, true)
        return mGetWaybillSkuList
    }


    val mUploadJdReceiveReport = MutableLiveData<BaseResult<Any>>()
    fun uploadJdReceiveReport(
        param: Param,
        picList: MutableList<String>
    ): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mUploadJdReceiveReport.value = homeRepository.uploadJdReceiveReport(
                param,
                picList
            )
            if (!mUploadJdReceiveReport.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mUploadJdReceiveReport.value!!.errorMsg)
            }
        }, true)
        return mUploadJdReceiveReport
    }


    val mGetIntransitTruck = MutableLiveData<BaseResult<MutableList<CarReceiveBean>>>()
    fun getIntransitTruck(param: Param): MutableLiveData<BaseResult<MutableList<CarReceiveBean>>> {
        launchGo({
            mGetIntransitTruck.value = homeRepository.getIntransitTruck(param)
            if (!mGetIntransitTruck.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetIntransitTruck.value!!.errorMsg)
            }
        }, true)
        return mGetIntransitTruck
    }

    val mGetSelfSendPlan = MutableLiveData<BaseResult<MutableList<DeliveryBean>>>()
    fun selfSendPlan(param: Param): MutableLiveData<BaseResult<MutableList<DeliveryBean>>> {
        launchGo({
            mGetSelfSendPlan.value = homeRepository.selfSendPlan(param)
            if (!mGetSelfSendPlan.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetSelfSendPlan.value!!.errorMsg)
            }
        }, true)
        return mGetSelfSendPlan
    }

    val mGetIntransitTruckDetail = MutableLiveData<BaseResult<MutableList<CarReceiveDetailBean>>>()
    fun getIntransitTruckDetail(param: Param): MutableLiveData<BaseResult<MutableList<CarReceiveDetailBean>>> {
        launchGo({
            mGetIntransitTruckDetail.value = homeRepository.getIntransitTruckDetail(param)
            if (!mGetIntransitTruckDetail.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetIntransitTruckDetail.value!!.errorMsg)
            }
        }, true)
        return mGetIntransitTruckDetail
    }

    val mDeliveryReceiptCreate = MutableLiveData<BaseResult<DocumentDetail>>()
    fun deliveryReceiptCreate(param: Param): MutableLiveData<BaseResult<DocumentDetail>> {
        launchGo({
            mDeliveryReceiptCreate.value = homeRepository.deliveryReceiptCreate(param)
            if (!mDeliveryReceiptCreate.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mDeliveryReceiptCreate.value!!.errorMsg)
            }
        }, true)
        return mDeliveryReceiptCreate
    }


    val mUploadDriverDeliveryReceipt = MutableLiveData<BaseResult<Any>>()
    fun uploadDriverDeliveryReceipt(
        param: Param,
        picList: MutableList<String>
    ): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mUploadDriverDeliveryReceipt.value = homeRepository.uploadDriverDeliveryReceipt(
                param,
                picList
            )
            if (!mUploadDriverDeliveryReceipt.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mUploadDriverDeliveryReceipt.value!!.errorMsg)
            }
        }, true)
        return mUploadDriverDeliveryReceipt
    }

    val mUploadDriverDeliveryImgUp = MutableLiveData<BaseResult<Any>>()
    fun uploadDriverDeliveryImgUp(
        param: Param,
        picList: MutableList<String>
    ): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mUploadDriverDeliveryImgUp.value = homeRepository.uploadDriverDeliveryImgUp(
                param,
                picList
            )
            if (!mUploadDriverDeliveryImgUp.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mUploadDriverDeliveryImgUp.value!!.errorMsg)
            }
        }, true)
        return mUploadDriverDeliveryImgUp
    }


    val mAddHandingFee = MutableLiveData<BaseResult<Any>>()
    fun addHandingFee(
        param: Param,
        picList: MutableList<String>
    ): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mAddHandingFee.value = homeRepository.addHandingFee(
                param,
                picList
            )
            if (!mAddHandingFee.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mAddHandingFee.value!!.errorMsg)
            }
        }, true)
        return mAddHandingFee
    }


    val mUploadReturnsStoreDeliveryReceipt = MutableLiveData<BaseResult<Any>>()
    fun uploadReturnsStoreDeliveryReceipt(
        param: Param,
        picList: MutableList<String>
    ): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mUploadReturnsStoreDeliveryReceipt.value =
                homeRepository.uploadReturnsStoreDeliveryReceipt(
                    param,
                    picList
                )
            if (!mUploadReturnsStoreDeliveryReceipt.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mUploadReturnsStoreDeliveryReceipt.value!!.errorMsg)
            }
        }, true)
        return mUploadReturnsStoreDeliveryReceipt
    }

    val mGetReturnsSkuListByWaybill = MutableLiveData<BaseResult<ReturnSkuBean>>()
    fun getReturnsSkuListByWaybill(param: Param): MutableLiveData<BaseResult<ReturnSkuBean>> {
        launchGo({
            mGetReturnsSkuListByWaybill.value = homeRepository.getReturnsSkuListByWaybill(param)
            if (!mGetReturnsSkuListByWaybill.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetReturnsSkuListByWaybill.value!!.errorMsg)
            }
        }, true)
        return mGetReturnsSkuListByWaybill
    }

    val mWaybillDetail = MutableLiveData<BaseResult<List<HandingFeeWaybillList>>>()
    fun waybillDetail(param: Param): MutableLiveData<BaseResult<List<HandingFeeWaybillList>>> {
        launchGo({
            mWaybillDetail.value = homeRepository.waybillDetail(param)
            if (!mWaybillDetail.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mWaybillDetail.value!!.errorMsg)
            }
        }, true)
        return mWaybillDetail
    }

    val mGetSkuListByWaybill = MutableLiveData<BaseResult<MutableList<SkuListBean>>>()
    fun getSkuListByWaybill(param: Param): MutableLiveData<BaseResult<MutableList<SkuListBean>>> {
        launchGo({
            mGetSkuListByWaybill.value = homeRepository.getSkuListByWaybill(param)
            if (!mGetSkuListByWaybill.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetSkuListByWaybill.value!!.errorMsg)
            }
        }, true)
        return mGetSkuListByWaybill
    }

    val mSendReturnsPackingList = MutableLiveData<BaseResult<CartonResponseBean>>()
    fun sendReturnsPackingList(param: Param): MutableLiveData<BaseResult<CartonResponseBean>> {
        launchGo({
            mSendReturnsPackingList.value = homeRepository.sendReturnsPackingList(param)

            if (!mSendReturnsPackingList.value!!.isSuccess()) {
                defUI.toastEvent.postValue(
                    if (mSendReturnsPackingList.value!!.errorCode == GlobalConfig.JSON_RESULT_SKU_ERROR)
                        "${mSendReturnsPackingList.value!!.errorMsg}  SKU数量异常" else
                        mSendReturnsPackingList.value!!.errorMsg
                )
            }
        }, true)
        return mSendReturnsPackingList
    }


    val mModifyReturnsWaybill = MutableLiveData<BaseResult<Any>>()
    fun modifyReturnsWaybill(param: Param): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mModifyReturnsWaybill.value = homeRepository.modifyReturnsWaybill(param)
            if (!mModifyReturnsWaybill.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mModifyReturnsWaybill.value!!.errorMsg)
            }
        }, true)
        return mModifyReturnsWaybill
    }


    val mGetReturnsScanList = MutableLiveData<BaseResult<ScanDataBean>>()
    fun getReturnsScanList(param: Map<String, String>): MutableLiveData<BaseResult<ScanDataBean>> {
        launchGo({
            mGetReturnsScanList.value = homeRepository.getReturnsScanList(param)
            if (!mGetReturnsScanList.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetReturnsScanList.value!!.errorMsg)
            }
        }, true)
        return mGetReturnsScanList
    }

    val mSendReturnsScanLog = MutableLiveData<BaseResult<Any>>()
    val mReturnsScanCarton = MutableLiveData<MutableList<ScanCarton>>()
    fun sendReturnsScanLog(param: MutableList<ScanCarton>): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mSendReturnsScanLog.value = homeRepository.sendReturnsScanLog(param)
            if (!mSendReturnsScanLog.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mSendReturnsScanLog.value!!.errorMsg)
            }else{
                mReturnsScanCarton.value = param
            }
        }, false)
        return mSendReturnsScanLog
    }

    val mForwarderList = MutableLiveData<BaseResult<MutableList<ForwarderListBean>>>()
    fun forwarderList(): MutableLiveData<BaseResult<MutableList<ForwarderListBean>>> {
        launchGo({
            mForwarderList.value = homeRepository.forwarderList()
            if (!mForwarderList.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mForwarderList.value!!.errorMsg)
            }
        }, true)
        return mForwarderList
    }


    val mQueryWaybillByCity = MutableLiveData<BaseResult<MutableList<WaybillCityBean>>>()
    fun queryWaybillByCity(param: Param): MutableLiveData<BaseResult<MutableList<WaybillCityBean>>> {
        launchGo({
            mQueryWaybillByCity.value = homeRepository.queryWaybillByCity(param)
            if (!mQueryWaybillByCity.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mQueryWaybillByCity.value!!.errorMsg)
            }
        }, true)
        return mQueryWaybillByCity
    }


    val mGetReturnsCartonDetail = MutableLiveData<BaseResult<CartonResponseBean>>()
    fun getReturnsCartonDetail(param: Param): MutableLiveData<BaseResult<CartonResponseBean>> {
        launchGo({
            mGetReturnsCartonDetail.value = homeRepository.getReturnsCartonDetail(param)
            if (!mGetReturnsCartonDetail.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetReturnsCartonDetail.value!!.errorMsg)
            }
        }, true)
        return mGetReturnsCartonDetail
    }


    val mGetSkuByChannelOrder = MutableLiveData<BaseResult<MutableList<SkuBean>>>()
    fun getSkuByChannelOrder(param: Param): MutableLiveData<BaseResult<MutableList<SkuBean>>> {
        launchGo({
            mGetSkuByChannelOrder.value = homeRepository.getSkuByChannelOrder(param)
            if (!mGetSkuByChannelOrder.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetSkuByChannelOrder.value!!.errorMsg)
            }
        }, true)
        return mGetSkuByChannelOrder
    }


    val mGetWareFileBySku = MutableLiveData<BaseResult<MutableList<String>>>()
    fun getWareFileBySku(param: Param): MutableLiveData<BaseResult<MutableList<String>>> {
        launchGo({
            mGetWareFileBySku.value = homeRepository.getWareFileBySku(param)
            if (!mGetWareFileBySku.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetWareFileBySku.value!!.errorMsg)
            }
        }, true)
        return mGetWareFileBySku
    }


    val mGetEntrustBookByCartonCode = MutableLiveData<BaseResult<EntrustBean>>()
    fun getEntrustBookByCartonCode(param: Param): MutableLiveData<BaseResult<EntrustBean>> {
        launchGo({
            mGetEntrustBookByCartonCode.value = homeRepository.getEntrustBookByCartonCode(param)
            if (!mGetEntrustBookByCartonCode.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetEntrustBookByCartonCode.value!!.errorMsg)
            }
        }, true)
        return mGetEntrustBookByCartonCode
    }


    val mGetCityNameList = MutableLiveData<BaseResult<CarCityBean>>()
    fun getCityNameList(): MutableLiveData<BaseResult<CarCityBean>> {
        launchGo({
            mGetCityNameList.value = homeRepository.getCityNameList()
            if (!mGetCityNameList.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetCityNameList.value!!.errorMsg)
            }
        }, true)
        return mGetCityNameList
    }


    val mRelationCity = MutableLiveData<BaseResult<Any>>()
    fun relationCity(param: Param): MutableLiveData<BaseResult<Any>> {
        launchGo({
            mRelationCity.value = homeRepository.relationCity(param)
            if (!mRelationCity.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mRelationCity.value!!.errorMsg)
            }
        }, true)
        return mRelationCity
    }


    val mGetNoCheckWaybillCarList = MutableLiveData<BaseResult<CarViewBean>>()
    fun getNoCheckWaybillCarList(): MutableLiveData<BaseResult<CarViewBean>> {
        launchGo({
            mGetNoCheckWaybillCarList.value = homeRepository.getNoCheckWaybillCarList()
            if (!mGetNoCheckWaybillCarList.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mGetNoCheckWaybillCarList.value!!.errorMsg)
            }
        }, true)
        return mGetNoCheckWaybillCarList
    }


    val mCarWaybillList = MutableLiveData<BaseResult<CarViewDetailBean>>()
    fun carWaybillList(param: Param): MutableLiveData<BaseResult<CarViewDetailBean>> {
        launchGo({
            mCarWaybillList.value = homeRepository.carWaybillList(param)
            if (!mCarWaybillList.value!!.isSuccess()) {
                defUI.toastEvent.postValue(mCarWaybillList.value!!.errorMsg)
            }
        }, true)
        return mCarWaybillList
    }


}